/*                                  ATELIER                                   */

/* %%LICENSE_TAG%%                                                            */
package edu.gmu.view.cube;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * The ControlPanel sets the various parameters for a 3D-coordinate system.
 * @author  James H. Pope
 * @version $Revision:$ $Date:$
 */
public class PanelRotation extends JPanel
{
    /**
     * Defaults
     */
    public static final int DEFAULT_XROTATION = 45; // in degrees
    public static final int DEFAULT_YROTATION = 45; // in degrees
    public static final int DEFAULT_ZROTATION = 45; // in degrees
    
    // The object that is interested in the values we produce
    private ControlModel controlModel = null;

    //-----------------------  Rotation Controls  ----------------------------//
    /**
     * xRotation
     */
    private JLabel xRotateLabel    = null;
    private JSpinner xRotateField  = null;

    /**
     * yRotation
     */
    private JLabel yRotateLabel    = null;
    private JSpinner yRotateField  = null;

    /**
     * zRotation
     */
    private JLabel zRotateLabel    = null;
    private JSpinner zRotateField  = null;


    /**
     * Creates a new instance of ControlPanel with specified values.
     * @param controlModel
     */
    public PanelRotation( ControlModel pControlModel )
    {
        this.controlModel = pControlModel;
        
        //--------------------  Rotation Controls  ---------------------------//
        // Create the labels/fields
        this.xRotateLabel     = new JLabel( "X: " );
        final SpinnerNumberModel xRotateModel = new SpinnerNumberModel(controlModel.getXRotation(), 0.0, 360.0, 5.0);
        xRotateModel.addChangeListener(new ChangeListener()
            {
                public void stateChanged(ChangeEvent ce)
                {
                    double newValue = xRotateModel.getNumber().doubleValue();
                    controlModel.setXRotation( Math.toRadians(newValue) );
                }
            }
        );
        this.xRotateField     = new JSpinner( xRotateModel );

        this.yRotateLabel     = new JLabel( "Y: " );
        final SpinnerNumberModel yRotateModel = new SpinnerNumberModel(controlModel.getYRotation(), 0.0, 360.0, 5.0);
        yRotateModel.addChangeListener(new ChangeListener()
            {
                public void stateChanged(ChangeEvent ce)
                {
                    double newValue = yRotateModel.getNumber().doubleValue();
                    controlModel.setYRotation( Math.toRadians(newValue) );
                }
            }
        );
        this.yRotateField     = new JSpinner( yRotateModel );

        this.zRotateLabel     = new JLabel( "Z: " );
        final SpinnerNumberModel zRotateModel = new SpinnerNumberModel(controlModel.getZRotation(), 0.0, 360.0, 5.0);
        zRotateModel.addChangeListener(new ChangeListener()
            {
                public void stateChanged(ChangeEvent ce)
                {
                    double newValue = zRotateModel.getNumber().doubleValue();
                    controlModel.setZRotation( Math.toRadians(newValue) );
                }
            }
        );
        this.zRotateField     = new JSpinner( zRotateModel );

    
        // Add the labels/fields to the panel
        JPanel rotationPanel = new JPanel();
        rotationPanel.setBorder(BorderFactory.createTitledBorder( "Rotate" ));
        BoxLayout rotationPanelLayout = new BoxLayout( rotationPanel, BoxLayout.Y_AXIS );
        rotationPanel.setLayout( rotationPanelLayout );
        rotationPanel.add( createRow( xRotateLabel,       xRotateField ) );
        rotationPanel.add( createRow( yRotateLabel,       yRotateField ) );
        rotationPanel.add( createRow( zRotateLabel,       zRotateField ) );


        this.setLayout( new BorderLayout() );
        this.add( rotationPanel, BorderLayout.CENTER );

    }

    /**
     *
     * @param label
     * @param field
     * @return
     */
    public JPanel createRow( JLabel label, JComponent field )
    {
    	JPanel rowPanel = new JPanel( );
    	Dimension labelDim = new Dimension( 100, 20 );
    	Dimension fieldDim = new Dimension( 150, 20 );

    	label.setPreferredSize( labelDim );
    	field.setPreferredSize( fieldDim );

    	rowPanel.add( label );
    	rowPanel.add( field );

    	return rowPanel;
    }

}

/*                                  ATELIER                                   */